package com.server.service;

import com.server.entity.UserExtendedInformation;
import com.server.entity.UserInfo;
import com.server.entity.UserInformation;
import com.server.example.UserExtendedInformationExample;
import com.server.example.UserInfoExample;
import com.server.example.UserInformationExample;
import com.server.mapper.UserExtendedInformationMapper;
import com.server.mapper.UserInfoMapper;
import com.server.mapper.UserInformationMapper;
import com.untils.MybatisClass;
import com.untils.SnowflakeIdWorker;
import org.apache.ibatis.session.SqlSession;

import java.util.Collections;
import java.util.List;

/**
 * 用户服务 service 类
 * @author Admin
 */
public class UserService {
    private final SnowflakeIdWorker idWorker = new SnowflakeIdWorker(SnowflakeIdWorker.getProcessID(), 0);
    /**
     * 用户登录：手机号登录/用户名登录
     * @param userInformation 传递的用户信息
     * @param userPhoneLogin 那种登录方式
     * @return 用户登录：手机号登录/用户名登录
     */
    public UserInformation userLogin(UserInformation userInformation, boolean userPhoneLogin){
        UserInformation user = new UserInformation();
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserInformationMapper userInformationMapper = sqlSession.getMapper(UserInformationMapper.class);
        UserInformationExample userInformationExample = new UserInformationExample();
        if(userPhoneLogin){
            userInformationExample.createCriteria().andUserNameEqualTo(userInformation.getUserName())
                    .andUserPasswordEqualTo(userInformation.getUserPassword());
        }else {
            userInformationExample.createCriteria().andUserPhoneEqualTo(userInformation.getUserPhone())
                    .andUserPasswordEqualTo(userInformation.getUserPassword());
        }
        try {
            user= userInformationMapper.selectByExample(userInformationExample).get(0);
            sqlSession.close();
        }catch (Exception e) {
            sqlSession.close();
            user=new UserInformation();
        }
        return user;
    }

    /**
     * 得到全部用户的个人基本信息/得到单个用户的个人基本信息
     * @param userid 用户id
     * @param isAdmin 是否为管理员，true 返回全部信息,false 返回用户的个人信息
     * @return 得到全部用户的个人信息/得到单个用户的个人信息
     */
    public List<UserInfo> getUserInfo(List<String> userid, boolean isAdmin) {
        List<UserInfo> users;
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfoExample userInfoExample = new UserInfoExample();
        if (isAdmin) {
            try {
                userInfoExample.createCriteria().andUserIdIsNotNull();
                users=userInfoMapper.selectByExample(userInfoExample);
                sqlSession.close();
            }catch (Exception e) {
                users=Collections.singletonList(new UserInfo());
                sqlSession.close();
//                return Collections.singletonList(new UserInformation());
            }
        }else {
            try {
                userInfoExample.createCriteria().andUserIdIn(userid);
                users=userInfoMapper.selectByExample(userInfoExample);
                sqlSession.close();
            }catch (Exception e) {
                users=Collections.singletonList(new UserInfo());
                sqlSession.close();
            }
        }
        return users;
    }

    /**
     * 用户注册功能实现
     * @param user 注册时候用户的传递信息
     * @return 返回是否成功 返回-1 未成功，1成功
     */
    public Integer userCreateUserInformation(UserInformation user) {
        SqlSession sqlSession = MybatisClass.getSqlSession();
        int sauces;
        UserInformationMapper userInformationMapper = sqlSession.getMapper(UserInformationMapper.class);
        try {
            user.setUserId(String.valueOf(idWorker.nextId()));
            sauces=userInformationMapper.insert(user);
            sqlSession.commit();
            sqlSession.close();
            return sauces;
        }catch (Exception e) {
            sqlSession.close();
            return -1;
        }
    }

    /**
     * 用户基础修改信息,用户的id，用户名，用户密码，用户手机号
     * @param user 修改时候用户的传递信息
     * @return 返回是否成功 返回-1 未成功，1成功
     */
    public Integer userUpdateUserInformation(UserInformation user) {
        SqlSession sqlSession = MybatisClass.getSqlSession();
        int sauces;
        UserInformationMapper userInformationMapper = sqlSession.getMapper(UserInformationMapper.class);
        UserInformationExample userInformationExample = new UserInformationExample();
        userInformationExample.createCriteria().andUserIdEqualTo(user.getUserId());
        try {
            sauces=userInformationMapper.updateByPrimaryKeySelective(user);
            System.out.println(sauces);
            sqlSession.commit();
            sqlSession.close();
            return sauces;
        }catch (Exception e) {
            sqlSession.close();
            return -1;
        }
    }

    /**
     * 用户扩展修改信息，姓名，简介，昵称，删除标记
     * @param users 修改的用户id 信息列表
     * @param user 修改时候用户的传递信息
     * @return 返回是否成功 返回-1 未成功，1成功
     */
    public Integer userUpdateUserInformation(List<String> users,UserExtendedInformation user) {
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserExtendedInformationMapper userExtendedInformationMapper = sqlSession.getMapper(UserExtendedInformationMapper.class);
        UserExtendedInformationExample userExtendedInformationExample = new UserExtendedInformationExample();
        userExtendedInformationExample.createCriteria().andUserIdIn(users);
        int sauces;
        try {
            sauces=userExtendedInformationMapper.updateByExampleSelective(user,userExtendedInformationExample);
            sqlSession.commit();
            sqlSession.close();
            return sauces;
        }catch (Exception e) {
            e.printStackTrace();
            sqlSession.close();
            return -1;
        }

    }
}
